<?php
namespace pgc;
use \hc\tpl;
use \hc\session;
use \hc\js;
class Notice extends tpl{
	public function __construct(){
		parent::__construct();
		session::init();
		if(isset($_SESSION['pgcinfo'])){
				$this->pgcinfo=$_SESSION['pgcinfo'];
		}else{
				$url=MODEL_URL."/Login/index.html";
			 echo 	js::to_url($url);exit;
		}
    $this->notice_m=new \model\notice_m;
	}
	public function index(){
    $where=array();
    $data=$this->notice_m->s_all_ajaxs($where);
    if(isset($_POST['ajaxs'])){
        ajax_return(true,'',$data);
    }
    $this->assign('listarr',$data['list']);
    $this->assign('pageshow',$data['pageshow']);
		 $this->display('notice/lists.tpl');
	}
	public function noticeAdd(){
		if(isset($_POST['ajaxs'])){
				$data=ajaxs_from($_POST['arr']);
				if(trim($data['title'])==''){
					ajax_return(false.'标题不能为空');
				}
				if(trim($data['content'])==''){
					ajax_return(false.'内容不能为空');
				}
				$data['sendtime']=time();
				$data['senduserid']=$this->pgcinfo['id'];
				$data['sendusername']=$this->pgcinfo['loginname'];
				$data['senduserface']=$this->pgcinfo['face'];
				if($this->notice_m->a_one($data)===false){
						addlog($this->pgcinfo,'失败','添加公告');
						ajax_return(false,'添加失败');
				}else{
					addlog($this->pgcinfo,'成功','添加公告');
						ajax_return(true,'添加成功');
				}
		}
		$this->display('notice/add.tpl');
	}
	public function noticeEdit(){
			if(isset($_GET['id'])){
				$where['id']=array('=',$_GET['id']);
				$list=$this->notice_m->s_one($where);
				if(!$list){
						echo js::alert('不存在公告');
				}
				$this->assign('list',$list);
				$this->display('notice/edit.tpl');
			}elseif(isset($_POST['ajaxs'])){
					$data=ajaxs_from($_POST['arr']);
					if(trim($data['content'])==''){
							ajax_return(false,'公告内容不能为空');
					}
					$where['id']=array('=',$data['id']);
					$data['senduserid']=$this->pgcinfo['id'];
					$data['sendusername']=$this->pgcinfo['loginname'];
					$data['senduserface']=$this->pgcinfo['face'];
					if($this->notice_m->e_one($where,$data)===false){
						addlog($this->pgcinfo,'失败','编辑公告id:'.$data['id']);
						ajax_return(false,'编辑失败');
					}else{
						addlog($this->pgcinfo,'成功','编辑公告id:'.$data['id']);
						ajax_return(true,'编辑成功');
					}
			}
	}
	public function noticeDel(){
		if(isset($_POST['id'])){
				$where['id']=array('=',$_POST['id']);
				if($this->notice_m->d_one($where)===false){
						addlog($this->pgcinfo,'失败','删除公告id:'.$_POST['id']);
						ajax_return(false,'删除失败');
				}else{
						addlog($this->pgcinfo,'成功','删除公告id:'.$_POST['id']);
						ajax_return(true,'删除成功');
				}
		}
	}

}
?>
